package com.lnlyj.media.ui;

import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.lnlyj.media.R;
import com.lnlyj.media.base.BaseGroupAdapter;
import com.lnlyj.media.data.MediaGroup;
import com.lnlyj.media.data.MediaItem;
import com.lnlyj.media.util.WindowUtil;

import java.util.Locale;

import androidx.recyclerview.widget.RecyclerView;

/**
 * wanglei 2020-04-17
 */
public class MediaAdapter extends BaseGroupAdapter<MediaGroup, RecyclerView.ViewHolder, MediaChildHolder> {


    private OnMediaAdapterClick onMediaAdapterClick;

    public MediaAdapter() {
        super(false);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateGroupViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    protected MediaChildHolder onCreateChildViewHolder(ViewGroup parent) {
        return new MediaChildHolder(View.inflate(parent.getContext(), R.layout.item_media_child, null));
    }

    @Override
    public void onBindGroup(RecyclerView.ViewHolder holder, int groupPosition) {

    }

    @Override
    public void onBindChild(MediaChildHolder holder, int groupPosition, int childPosition) {
        MediaItem item = getGroup(groupPosition).items.get(childPosition);

        Glide.with(holder.imageView.getContext()).load(item.getUri()).skipMemoryCache(false).placeholder(R.drawable.ic_image_default).crossFade(0).into(holder.imageView);
        int index = SelectionManager.getInstance().getIndex(item);

        if (index != -1) {
            holder.selectTv.setText(String.format(Locale.CHINA, "%d", index));
            holder.selectTv.setSelected(true);
        } else {
            holder.selectTv.setText(null);
            holder.selectTv.setSelected(false);
        }

        if (item.isVideo()) {
            holder.videoLayout.setVisibility(View.VISIBLE);
            holder.duration.setText(getTimeString(item.getDuration() / 1000));
        } else {
            holder.videoLayout.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener(view -> {
            if (onMediaAdapterClick != null) {
                onMediaAdapterClick.onMediaClick(item, childPosition);
            }
        });

        holder.selectTv.setOnClickListener(view -> {
            if (onMediaAdapterClick != null) {
                onMediaAdapterClick.onSelectClick(item);
            }
        });

    }

    @Override
    public int getChildCount(MediaGroup mediaGroup) {
        return mediaGroup.items.size();
    }

    public void setOnMediaAdapterClick(OnMediaAdapterClick onMediaAdapterClick) {
        this.onMediaAdapterClick = onMediaAdapterClick;
    }

    public interface OnMediaAdapterClick {
        void onMediaClick(MediaItem item, int position);
        void onSelectClick(MediaItem item);
    }

    private String getTimeString(long duration) {
        String time;
        String format;
        if (duration / 3600 > 0) {
            format = "%02d:%02d:%02d";
            time = String.format(format, duration / 3600, (duration%3600) / 60, duration % 60);
        } else {
            format = "%02d:%02d";
            time = String.format(format, (duration%3600) / 60, duration % 60);
        }

        return time;
    }
}
